package cn.org.xuanguang.kepler.hormos.orm.acl;

import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.AbstractOperatorParameter;

import java.util.ArrayList;
import java.util.List;

/**
 * 拦截器模式
 *
 * @param <T>
 */
public class IPolicyInterceptorChain<T> {

    private final List<IPolicy<T>> interceptors = new ArrayList<>();
    /**
     * 加入参数操作器
     */
    private AbstractOperatorParameter target;

    public void addInterceptor(IPolicy interceptor) {
        interceptors.add(interceptor);
    }

    public void execute() {
        for (IPolicy interceptor : interceptors) {
            interceptor.before(target);
        }
        //此处添加参数操作
        target.execute();
        for (int i = interceptors.size() - 1; i >= 0; i--) {
            interceptors.get(i).after();
        }
    }

    public void setTarget(AbstractOperatorParameter target) {
        this.target = target;
    }

    public AbstractOperatorParameter getTarget() {
        return target;
    }
}